<?php
/* florent test ecriture fichier/dossier
//create the directory if doesn't exists (should have write permissons)
if(!is_dir("../lola")) mkdir("../lola", 0755); 
//move the uploaded file
move_uploaded_file($_FILES['Filedata']['tmp_name'], "../lola/".$_FILES['Filedata']['name']);
chmod("../lola/".$_FILES['Filedata']['name'], 0777);
*/
?>
<?PHP
include_once "../php/ini.php";

$error = "";
$msg = "";
$tmp = $_FILES['Filedata']['tmp_name'];
$nom = $_FILES['Filedata']['name'];
$repertoireDestination = "../images/";


//---- ajout florent --------------------------------//
if($_GET['user_id']) {
	$user_id = $_GET['user_id']; 
} else {
	$user_id = $_SESSION['id_user'];
}
//$id_user=$_SESSION['id_user'];
//---- ajout florent --------------------------------//



// Calcul des nouvelles dimensions
    list ($width, $height) = getimagesize($tmp);
	
    if($width >= $height)
	{
		$new_width = 512;
		$new_height = $height*512/$width;
	}
	else{
		$new_width = $width*512/$height;
		$new_height = 512;
	}
    
    // Redimensionnement
    $image_new = @imagecreatetruecolor($new_width, $new_height)
    or die ("Cannot Initialize new GD image stream");


    $blanc = ImageColorAllocate($image_new, 255, 255, 255);
    $extention = explode('.', $nom);
    $extention = strtolower($extention[count($extention)-1]);

    
    $image = imagecreatefromjpeg($tmp);
    imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    dbconnect();
    $query = "SELECT * FROM images ORDER BY id_image DESC LIMIT 1;";
    $result = mysql_query($query);
    $tab = mysql_fetch_assoc($result);
    $id_image = $tab['id_image']+1;
    $nom = $user_id."_".$id_image.".jpg";
   
    // enregistrement
    imagejpeg($image_new, $repertoireDestination.$nom);
	imagedestroy($image_new);
	

$repertoirePreview = "../imagespreview/";
// Calcul des nouvelles dimensions

list ($width, $height) = getimagesize($repertoireDestination.$nom);
$new_width = ($width >= $height)?100:100/$width;
$new_height = ($height >= width)?100:100/$height;
// Redimensionnement
$image_new = @imagecreatetruecolor($new_width, $new_height)
or die ("Cannot Initialize new GD image stream");


$blanc = ImageColorAllocate($image_new, 255, 255, 255);
$extention = explode('.', $nom);
$extention = strtolower($extention[count($extention)-1]);

//if ($extention == 'jpeg' || $extention == 'jpg') {

	$image = imagecreatefromjpeg($repertoireDestination.$nom);
    imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    // enregistrement
    imagejpeg($image_new, $repertoirePreview.$nom);

/*} else if ($extention == 'png') {
	
	$image = imagecreatefrompng($repertoireDestination.$nom);
    imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagepng($image_new, $repertoirePreview.$nom);

} elseif ($extention == 'gif') {
	
	$image = imagecreatefromgif($repertoireDestination.$nom);
    imagecopyresampled($image_new, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    imagegif($image_new, $repertoirePreview.$nom);

} else {
	
	echo "Format incorrect.";
}*/

imagedestroy($image_new);



$sql =	"INSERT INTO `images`
		(`id_image` , `id_user` , `nom_image` , `coord_x` , `coord_y` , `coord_z` , `droit`, `posted` )
		VALUES ( NULL , $user_id, '".$nom."', '', '', '', 'private', 'non' );";

$sqlresults = mysql_query($sql);

if (!$sqlresults) {
	echo '{"status":"ERROR: '.$sql.'"}';
}
else {
	
	$sql2 = "SELECT * FROM `images` ORDER BY id_image DESC LIMIT 1;";
    $result = mysql_query($sql2);
    $tab = mysql_fetch_assoc($result);
    
    /*echo "{";
    echo "error: '".$error."',\n";
    echo "nom: '".$nom."',\n";
	echo "id: '".$tab['id_image']."'\n";
    echo "}";*/
    
    //sended to flash to execute a js function
    echo $tab['id_image'].",".$tab['nom_image'].",".$tab['id_user'];
}

dbclose();
?>